import { call, put } from 'redux-saga/effects';
import { getRoles, addRole, deleteRole, editRole } from '../services/role';

export default {
  namespace: 'role',
  state: {
    roleList: {},
  },
  effects: {
    *getRoles({ payload }) {
      const response = yield call(getRoles, payload);
      yield put({
        type: 'role/getRolesList',
        payload: response,
      });
    },
    *addRole({ payload }) {
      const response = yield call(addRole, payload);
      yield put({
        type: 'role/getRoles',
        payload: response,
      });
    },
    *deleteRole({ payload }) {
      const response = yield call(deleteRole, payload);
      yield put({
        type: 'role/getRoles',
        payload: response,
      });
    },
    *editRole({ payload }) {
      const response = yield call(editRole, payload);
      yield put({
        type: 'role/getRoles',
        payload: response,
      });
    },
  },
  reducers: {
    getRolesList(state, action) {
      return {
        ...state,
        roleList: action.payload,
      };
    },
  },
};
